package Chapter01.Homework;

import java.sql.*;
// 2: 统计学生总数。并输出。
public class Work2 {
    public static void main(String[] args) {
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        String url="jdbc:mysql://localhost:3306/kgc76?useSSL=false";
        Connection connection=null;
        Statement st=null;
        ResultSet rs=null;
        try {
            connection= DriverManager.getConnection(url,"root","123456");
            System.out.println(connection.getClass().getName());
            System.out.println("连接已经建立");
            st=connection.createStatement();
            System.out.println("上下文："+st.getClass().getName());
            rs=st.executeQuery("select count(*) from s_student");
            while (rs.next()){
                int zhi=rs.getInt(1);
                System.out.println("学生总数:"+zhi);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                st.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
